﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using MiniX.API.Data;
using MiniX.Models;
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Senparc.CO2NET.Extensions;
using MiniX.Utility.Helper;

namespace MiniX.API.Controllers
{
    /// <summary>
    /// 意见反馈类，定义了CURD相关操作
    /// </summary>
    [Produces("application/json")]
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class FeedbackController : BaseController<FeedbackController>
    {
        /// <summary>
        /// FeedbackController
        /// </summary>
        public FeedbackController(MiniXApiContext context, IHttpContextAccessor httpContextAccessor) : base(context, httpContextAccessor)
        {

        }

        /// <summary>
        /// 新增意见反馈
        /// </summary>
        /// <param name="feedback">构造意见反馈对象数据</param>
        /// <returns>返回新增的意见反馈</returns>          
        [HttpPost]
        [Route("/api/feedback")]
        public async Task<IActionResult> Create(Feedback feedback)
        {
            try
            {
                log.Information("feedback=" + feedback.ToJson());
                var img_list = feedback.feedback_remark;

                feedback.feedback_remark = "";
                feedback.client_ip = HttpContext.Connection.RemoteIpAddress!.ToString();
                feedback.created_at = DateTime.Now;
                _context.mini_feedback.Add(feedback);
                await _context.SaveChangesAsync();

                // 处理附件
                try
                {
                    if (!string.IsNullOrEmpty(img_list))
                    {
                        if (img_list.Contains(","))
                        {
                            var imgs = img_list.Split(new char[] { ',' });
                            for (int i = 0; i < imgs.Length; i++)
                            {
                                _context.mini_attachments.Add(new Attachments()
                                {
                                    object_id = feedback.feedback_id,
                                    type_id = 3,
                                    attachment_name = "",
                                    attachment_path = imgs[i],
                                    attachment_remark = ""
                                });
                                _context.SaveChanges();
                            }
                        }
                        else
                        {
                            _context.mini_attachments.Add(new Attachments()
                            {
                                object_id = feedback.feedback_id,
                                type_id = 3,
                                attachment_name = "",
                                attachment_path = img_list,
                                attachment_remark = ""
                            });
                            _context.SaveChanges();
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Fatal(ex, " -> 添加附件");
                }

                return Json(new { code = 0, msg = "提交成功，感谢你的反馈！", data = feedback });
            }
            catch (Exception ex)
            {
                log.Fatal(ex, " -> Create");
                return Json(new { code = 1, errcode = (int)ENUMHelper.ExceptionType.Create, errmsg = ex.Message });
            }
        }
    }
}
